package com.yang.guliware.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.guliware.entity.WmsWareInfo;
import com.yang.guliware.service.impl.WmsWareInfoServiceImpl;
import com.yang.guliware.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 仓库信息 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-02
 */
@RestController
@RequestMapping("/guliware/wms-ware-info")
public class WmsWareInfoController {

    @Autowired
    private WmsWareInfoServiceImpl wmsWareInfoService;

    @PostMapping("wareInfoList/{current}/{limit}")
    public R wareInfoList(@PathVariable Long current, @PathVariable Long limit,
                          @RequestBody(required = false) WmsWareInfo wmsWareInfo){
        QueryWrapper<WmsWareInfo> queryWrapper = new QueryWrapper<>();
        Page<WmsWareInfo> page = new Page<>(current,limit);
        String name = wmsWareInfo.getName();
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        wmsWareInfoService.page(page,queryWrapper);
        long total = page.getTotal();
        List<WmsWareInfo> records = page.getRecords();
        return R.ok().data("total",total).data("records",records);
    }

    @PostMapping("addWare")
    public R addWare(@RequestBody WmsWareInfo wmsWareInfo){
        wmsWareInfoService.save(wmsWareInfo);
        return R.ok();
    }

    @PostMapping("updateWare")
    public R updateWare(@RequestBody WmsWareInfo wmsWareInfo){
        wmsWareInfoService.updateById(wmsWareInfo);
        return R.ok();
    }

    @DeleteMapping("removeById/{id}")
    public R removeById(@PathVariable Long id){
        wmsWareInfoService.removeById(id);
        return R.ok();
    }

    @GetMapping("selectById/{id}")
    public R selectById(@PathVariable Long id){
        WmsWareInfo wareInfo = wmsWareInfoService.getById(id);
        return R.ok().data("wareInfo",wareInfo);
    }
    @GetMapping("getListWare")
    public R getListWare(){
        QueryWrapper<WmsWareInfo> queryWrapper = new QueryWrapper<>();
        List<WmsWareInfo> list = wmsWareInfoService.list(queryWrapper);
        return R.ok().data("list",list);
    }

}

